<div class="content-wrapper">

    <section class="content-header">
        <?php echo $this->partial('layout/breadcrumb'); ?>

        <h1 class="pull-right">
            <?php echo $this->escapeHtml($this->otherTagInfo->getOtherTagName()); ?>
            <a href="<?php echo $this->url('goods-other-tag');?>" class="btn btn-default btn-sm"><i class="fa fa-arrow-left"></i> <?php echo $this->translate('返回聚合标签列表'); ?></a>
            <button type="button" onclick="addOtherTagGoods();" class="btn btn-primary btn-sm"><i class="fa fa-plus"></i> <?php echo $this->translate('添加商品'); ?></button>
            <button type="button" onclick="updateGoodsSort();" class="btn btn-primary btn-sm"><i class="fa fa-check"></i> <?php echo $this->translate('更新排序'); ?></button>
            <?php echo $this->HelpUrl('goodsOtherTag'); ?>
        </h1>

    </section>

    <section class="content">
        <?php echo $this->partial('layout/messages'); ?>

        <div class="row">
            <div class="col-xs-12">
                <div class="box shop-box">
                    <form method="post" action="<?php echo $this->url('goods-other-tag', ['action' => 'updateOtherTagGoodsSort']);?>" name="edit-all-form" id="edit-all-form">
                    <div class="box-body table-responsive no-padding">
                        <table class="table shop-table table-hover">
                            <thead>
                            <tr class="shop-bg-thead">
                                <th style="width: 50%"><?php echo $this->translate('商品名称'); ?></th>
                                <th style="width: 20%"><?php echo $this->translate('商品状态'); ?></th>
                                <th style="width: 15%"><?php echo $this->translate('商品排序'); ?></th>
                                <th style="width: 10%"><?php echo $this->translate('操作'); ?></th>
                            </tr>
                            </thead>
                            <tbody id="insert-goods">
                            <?php if ($this->otherTagGoodsList) foreach ($this->otherTagGoodsList as $goodsValue) { ?>
                                <tr id="in-goods-<?php echo $goodsValue[0]->getOtherTagGoodsId(); ?>">
                                    <td><?php echo $this->escapeHtml($goodsValue['goodsName']); ?></td>
                                    <td><?php echo $this->goodsHelper()->goodsState($goodsValue['goodsState']); ?></td>
                                    <td><input type="number" min="1" name="tagGoodsSort[<?php echo $goodsValue[0]->getOtherTagGoodsId(); ?>]" value="<?php echo $goodsValue[0]->getOtherTagGoodsSort(); ?>" class="form-control input-sm"></td></td>
                                    <td><a href="javascript:;" onclick="deleteNotConfirm('<?php echo $this->url('goods-other-tag', ['action'=>'deleteOtherTagGoods', 'id' => $goodsValue[0]->getOtherTagGoodsId()]); ?>', 'in-goods-<?php echo $goodsValue[0]->getOtherTagGoodsId(); ?>');" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i> <?php echo $this->translate('删除'); ?></a></td>
                                </tr>
                            <?php } ?>
                            </tbody>
                        </table>
                    </div>
                    <div class="box-footer clearfix">
                        <button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-check"></i> <?php echo $this->translate('更新排序'); ?></button>
                    </div>
                    </form>
                </div>
            </div>
        </div>
    </section>

    <div class="modal fade" id="modalDefault">
        <div class="modal-dialog" style="width: 800px;" id="addGoodsListBody">

        </div>
    </div>

</div>
<script type="text/javascript">
    function addOtherTagGoods() {
        $('#modalDefault').modal('show');
        dbshopAjaxList('<?php echo $this->url('goods', ['action' => 'ajaxGoodsSearch']);?>', 'addGoodsListBody');
    }
    function searchGoods() {
        dbshopAjaxList('<?php echo $this->url('goods', ['action' => 'ajaxGoodsSearch']);?>?searchGoodsName='+$('#searchGoodsName').val(), 'addGoodsListBody');
    }
    function addSelectGoods(goodsId) {
        if(goodsId == '') {
            shopMessage('<?php echo $this->translate('请选择需要添加的预售商品!')?>');
        } else {
            let insertState = true;
            $("input[name='goodsId[]']").each(function () {
                if($(this).val() == goodsId) {
                    insertState = false;
                }
            });
            if(!insertState) {
                return false;
            }
            $.post("<?php echo $this->url('goods-other-tag', ['action' => 'addOtherTagGoods']); ?>", {goodsId: goodsId, otherTagId:<?php echo $this->otherTagInfo->getOtherTagId(); ?>}, function (data) {
                if(data.state == 'ok') {
                    let htmlStr = '<tr id="in-goods-'+data.result.otherTagGoodsId+'">';
                    htmlStr += '<td>'+data.result.goodsName+'</td>';
                    htmlStr += '<td>'+data.result.goodsState+'</td>';
                    htmlStr += '<td><input type="number" min="1" name="tagGoodsSort['+data.result.otherTagGoodsId+']" value="'+data.result.otherTagGoodsSort+'" class="form-control input-sm"></td>';
                    htmlStr += '<td><a href="javascript:;" onclick="deleteNotConfirm(\'<?php echo $this->url('goods-other-tag', ['action'=>'deleteOtherTagGoods']); ?>/'+data.result.otherTagGoodsId+'\', \'in-goods-'+data.result.otherTagGoodsId+'\');" class="btn btn-xs btn-danger"><i class="fa fa-trash"></i> <?php echo $this->translate('删除'); ?></a></td>';
                    htmlStr += '</tr>';

                    $('#insert-goods').prepend(htmlStr);
                } else {
                    shopMessage('<?php echo  $this->translate('该商品已经添加!')?>');
                }
            }, 'json');
        }
    }
    function addMoreSelectGoods() {
        $("input[name='selectGoodsId[]']").each(function () {
            if(this.checked) {
                addSelectGoods(this.value);
            }
        });
        $('#modalDefault').modal('hide');
    }

    function updateGoodsSort() {
        $('#edit-all-form').submit();
    }
</script>